package com.oraking.android.hybridapp.dev.korealaw.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.util.Log;

/**
 * 파일명          : CmmFileUtil.java 
 * 작성일          : 2011. 9. 30.
 * 작성자          : <a href='http://oraking.springnote.com/' target='_blank'>Teddy</a>
 * Todo      :
 * 수정내용
 * -------------------------------------------------------------
 *      수정일      |      수정자      |              수정내용              
 * -------------------------------------------------------------
 *              |              |
 */
public class CmmFileUtil {
	
	public static void writeFile(String location, String fileName, String contents){
		
		File f = new File(location);
		f.mkdirs();
		
		File file = new File(location + fileName);
		
		boolean exists = file.exists();
		if (exists == true){
			file.deleteOnExit();
		}
		
		FileWriter fw = null;
		
		try {
			fw = new FileWriter(file);
			fw.write(contents);
			fw.flush();
			
		}catch(IOException e){
			Log.d("error", "CmmFileUtil writeFile Error");
			Log.d("error", e.getMessage());
		}
		try {
			fw.close();
		} catch (Exception e) {
		}
		

	}
	
	// location - "data/data/패키지명/..", path - assets 폴더내의 Path, files - 삭제할 파일들의 이름
	public static void delFile(String location, String path, String[] files, Activity activity){
		boolean exists;
		File file;
		
		for(String file_name: files){
			exists = (new File(location + file_name)).exists();
			
			if(exists == true){
				file = new File(location+file_name);
				file.delete();
			}
		}
	}
	
	// location - "data/data/패키지명/..", path - assets 폴더내의 Path, files - 복사할 파일들의 이름
	public static void chkFile(String location, String path, String[] files, Activity activity){
		
		boolean exists;
		
		delFile(location, path, files, activity);
		
		for(String file: files){
			exists = (new File(location + file)).exists();
			
			if(exists == false){
				copy(location, path, file, activity);
			}
		}
	}
	
	public static void copy(String location, String path, String file, Activity activity) {

		boolean exists = (new File(location + file)).exists();
		if (exists == true)
			return;

		File f = new File(location+path);
		f.mkdirs();

		InputStream is = null;
		BufferedInputStream bis = null;
		
		try {
			// 만약 assets폴더내에 폴더를 만드셧다면 .open("폴더명/"+file)로 하시면 됩니다.
			is = activity.getAssets().open(path+file);
			
			bis = new BufferedInputStream(is);
		} catch (IOException e) {
		}

		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			fos = new FileOutputStream(new File(location+path + file));
			bos = new BufferedOutputStream(fos);
		} catch (FileNotFoundException e) {
		}

		byte[] buffer = new byte[1024];
		int read;

		try {
			while ((read = bis.read(buffer, 0, 1024)) != -1) {
				bos.write(buffer, 0, read);
			}
		} catch (IOException e) {
		}

		try {
			bos.flush();
		} catch (IOException e) {
		}

		try {
			bos.close();
			fos.close();			
		} catch (IOException e) {
		}

		try {
			is.close();
			bis.close();
		} catch (IOException e) {
		}
	}

}
